<?php


 class TitleInsuranceCalculatorHelper {

   // private static TitleInsuranceCalculatorHelper $instance = new TitleInsuranceCalculatorHelper();
    private static $instance;
    static public  function getInstance() 
    {
        return $this->instance;
    }
    /**
     * Converts a dollars (and optionally cents) value to the number of thousands.  It will round up all partial
     * thousands.
     *
     * @param amount value to round
     * @return number of thousands
     */
    public function  roundUp2Thousands($amount) {
        return (int)(ceil($amount / 1000));
    }
    /**
     * Converts a dollars (and optionally cents) value to the number of half thousands.  It will round up all partial
     * half thousands.
     *
     * @param amount value to round
     * @return number of half thousands
     */
    public function  roundUp2HalfThousands($amount) {
        return (int)(ceil($amount / 500));
    }
    public function roundUp2Hundreds($amount) {
        return (int)(ceil($amount / 100));
    }
      /**
     * Rounds value to dollars and cents, i.e. $2.78.
     *
     * @param value       the value to round
     * @param numDecimals number of decimals to round to
     * @return dollars and cents, i.e. $2.78.
     */
    public function  round2($value, $numDecimals) {
        $result = $value * pow(10, $numDecimals);
        $result = round($result);
        $result = $result / pow(10, $numDecimals);
        return $result;
    }
     /**
     * Calculates a Cost for a given request.  This method uses an upto 10 tier Cost structure with a minimum Cost.  Each tier
     * is inclusive, that is if a tier had a TierCost where getTier() == 250 that means up-to and including 250.
     *
     * @param tierValue the dollar amount of the title insurance in thousands.
     * @param tierCosts the 6 tier Costs (inclusive at each tier).
     * @param minCost   the minimum amount for any title insurance.
     * @return title insurance Cost
     */
    public function calculateTieredCost($tierValue, $tierCosts, $minCost) {

        $tierSize = count($tierCosts);
        if ($tierSize > 10) {
            return "tierCosts size must be 10 or less";
        }

//         $tier0Level = 0;
//         $tier0Cost = 0;
//         $tier1Level = 0;
//         $tier1Cost = 0;
//         $tier2Level = 0;
//         $tier2Cost = 0;
//         $tier3Level = 0;
//         $tier3Cost = 0;
//         $tier4Level = 0;
//         $tier4Cost = 0;
//         $tier5Level = 0;
//         $tier5Cost = 0;
//         $tier6Level = 0;
//         $tier6Cost = 0;
//         $tier7Level = 0;
//         $tier7Cost = 0;
//         $tier8Level = 0;
//         $tier8Cost = 0;
//         $tier9Level = 0;
//         $tier9Cost = 0;
//         
//        if ($tierSize > 0) {
//            $tier0Level = $tierCosts[0]->getTier();
//            $tier0Cost =  $tierCosts[0]->getCost();
//            if ($tierSize > 1) {
//                $tier1Level = $tierCosts[1]->getTier();
//                $tier1Cost =  $tierCosts[1]->getCost();
//                if ($tierSize > 2) {
//                    $tier2Level = $tierCosts[2]->getTier();
//                    $tier2Cost =  $tierCosts[2]->getCost();
//                    if ($tierSize > 3) {
//                        $tier3Level = $tierCosts[3]->getTier();
//                        $tier3Cost = $tierCosts[3]->getCost();
//                        if ($tierSize > 4) {
//                            $tier4Level = $tierCosts[4]->getTier();
//                            $tier4Cost = $tierCosts[4]->getCost();
//                            if (tierSize > 5) {
//                                $tier5Level = $tierCosts[5]->getTier();
//                                $tier5Cost = $tierCosts[5]->getCost();
//                                if ($tierSize > 6) {
//                                    $tier6Level = $tierCosts[6]->getTier();
//                                    $tier6Cost = $tierCosts[6]->getCost();
//                                    if ($tierSize > 7) {
//                                        $tier7Level = $tierCosts[7]->getTier();
//                                        $tier7Cost = $tierCosts[7]->getCost();
//                                        if ($tierSize > 8) {
//                                            $tier8Level = $tierCosts[8]->getTier();
//                                            $tier8Cost = $tierCosts[8]->getCost();
//                                            if ($tierSize > 9) {
//                                                $tier9Level = $tierCosts[9]->getTier();
//                                                $tier9Cost = $tierCosts[9]->getCost();
//                                            }
//                                        }
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//        }
//
//        $Cost = 0;
//        for ($i = 1; $i <= $tierValue; $i++) {
//            switch ($tierSize) {
//                case 1:
//                    if ($i <= $tier0Level) {
//                        $Cost += $tier0Cost;
//                    }
//                    break;
//                case 2:
//                    if ($i <= $tier0Level) {
//                        $Cost += $tier0Cost;
//                    } else if ($i <= $tier1Level) {
//                        $Cost += $tier1Cost;
//                    }
//                    break;
//                case 3:
//                    if ($i <= $tier0Level) {
//                        $Cost += $tier0Cost;
//                    } else if ($i <= $tier1Level) {
//                        $Cost += $tier1Cost;
//                    } else if ($i <= $tier2Level) {
//                        $Cost += $tier2Cost;
//                    }
//                    break;
//                case 4:
//                    if ($i <= $tier0Level) {
//                        $Cost += $tier0Cost;
//                    } else if ($i <= $tier1Level) {
//                        $Cost += $tier1Cost;
//                    } else if ($i <= $tier2Level) {
//                        $Cost += $tier2Cost;
//                    } else if ($i <= $tier3Level) {
//                        $Cost += $tier3Cost;
//                    }
//                    break;
//                case 5:
//                    if ($i <= $tier0Level) {
//                        $Cost += $tier0Cost;
//                    } else if ($i <= $tier1Level) {
//                        $Cost += $tier1Cost;
//                    } else if ($i <= $tier2Level) {
//                        $Cost += $tier2Cost;
//                    } else if ($i <= $tier3Level) {
//                        $Cost += $tier3Cost;
//                    } else if ($i <= $tier4Level) {
//                        $Cost += $tier4Cost;
//                    }
//                    break;
//                case 6:
//                    if ($i <= $tier0Level) {
//                        $Cost += $tier0Cost;
//                    } else if ($i <= $tier1Level) {
//                        $Cost += $tier1Cost;
//                    } else if ($i <= $tier2Level) {
//                        $Cost += $tier2Cost;
//                    } else if ($i <= $tier3Level) {
//                        $Cost += $tier3Cost;
//                    } else if ($i <= $tier4Level) {
//                        $Cost += $tier4Cost;
//                    } else if ($i <= $tier5Level) {
//                        $Cost += $tier5Cost;
//                    }
//                    break;
//                case 7:
//                    if ($i <= $tier0Level) {
//                        $Cost += $tier0Cost;
//                    } else if ($i <= $tier1Level) {
//                        $Cost += $tier1Cost;
//                    } else if ($i <= $tier2Level) {
//                        $Cost += $tier2Cost;
//                    } else if ($i <= $tier3Level) {
//                        $Cost += $tier3Cost;
//                    } else if ($i <= $tier4Level) {
//                        $Cost += $tier4Cost;
//                    } else if ($i <= $tier5Level) {
//                        $Cost += $tier5Cost;
//                    } else if ($i <= $tier6Level) {
//                        $Cost += $tier6Cost;
//                    }
//                    break;
//                case 8:
//                    if ($i <= $tier0Level) {
//                        $Cost += $tier0Cost;
//                    } else if ($i <= $tier1Level) {
//                        $Cost += $tier1Cost;
//                    } else if ($i <= $tier2Level) {
//                        $Cost += $tier2Cost;
//                    } else if ($i <= $tier3Level) {
//                        $Cost += $tier3Cost;
//                    } else if ($i <= $tier4Level) {
//                        $Cost += $tier4Cost;
//                    } else if ($i <= $tier5Level) {
//                        $Cost += $tier5Cost;
//                    } else if ($i <= $tier6Level) {
//                        $Cost += $tier6Cost;
//                    } else if ($i <= $tier7Level) {
//                        $Cost += $tier7Cost;
//                    }
//                    break;
//                case 9:
//                    if ($i <= $tier0Level) {
//                        $Cost += $tier0Cost;
//                    } else if ($i <= $$tier1Level) {
//                        $Cost += $tier1Cost;
//                    } else if ($i <= $tier2Level) {
//                        $Cost += $tier2Cost;
//                    } else if ($i <= $tier3Level) {
//                        $Cost += $tier3Cost;
//                    } else if ($i <= $tier4Level) {
//                        $Cost += $tier4Cost;
//                    } else if ($i <= $tier5Level) {
//                        $Cost += $tier5Cost;
//                    } else if ($i <= $tier6Level) {
//                        $Cost += $tier6Cost;
//                    } else if ($i <= $tier7Level) {
//                        $Cost += $tier7Cost;
//                    } else if ($i <= $tier8Level) {
//                        $Cost += $tier8Cost;
//                    }
//                    break;
//                case 10:
//                    if ($i <= $tier0Level) {
//                        $Cost += $tier0Cost;
//                    } else if ($i <= $$tier1Level) {
//                        $Cost += $tier1Cost;
//                    } else if ($i <= $tier2Level) {
//                        $Cost += $tier2Cost;
//                    } else if ($i <= $tier3Level) {
//                        $Cost += $tier3Cost;
//                    } else if ($i <= $tier4Level) {
//                        $Cost += $tier4Cost;
//                    } else if ($i <= $tier5Level) {
//                        $Cost += $tier5Cost;
//                    } else if ($i <= $tier6Level) {
//                        $Cost += $tier6Cost;
//                    } else if ($i <= $tier7Level) {
//                        $Cost += $tier7Cost;
//                    } else if ($i <= $tier8Level) {
//                        $Cost += $tier8Cost;
//                    } else {
//
//                        if ($i <= $tier9Level) {
//                            $Cost += $tier9Cost;
//                        }
//                    }
//                    break;
//            }
//        }
//
//        $Cost = max($Cost, $minCost);
//        //echo $Cost; exit();
//        return $this->round2($Cost, 2);
        $Cost = 0;
        for ($i = 1; $i <= $tierValue; $i++)
        {
            for ($j = 0; $j < $tierSize; $j++)
                if ($i <= $tierCosts[$j]->getTier())
                {
                    $Cost += $tierCosts[$j]->getCost();
                    break;
                }
        }
        $Cost = max($Cost, $minCost);
        return $this->round2($Cost, 2);
    }

}
?>
